package sv.ues.fia.activities;

import sv.ues.fia.R;
import sv.ues.fia.fragmentcontrol.HomeFragment;
import sv.ues.fia.fragmentcontrol.MyFragmentPagerAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

public class HomeActivity extends FragmentActivity {
	/** Definir todos los objetos EN EL FRAGMENT **/
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_pager);

		ViewPager pager = (ViewPager) findViewById(R.id.pager); 
		FragmentManager fMananger = getSupportFragmentManager(); 
		/** Se crean dos instancias del mismo Fragment una por cada pagina **/
		HomeFragment fragmentPageOne = new HomeFragment();
		HomeFragment fragmentPageTwo = new HomeFragment();
		/** Obtener los titulos de pagina del Res->String para manejar idiomas **/
		String titlePageOne = getResources().getString(R.string.notifyTab);
		String titlePageTwo = getResources().getString(R.string.menuTab);
	
		
		MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(
				fMananger, fragmentPageOne, fragmentPageTwo, titlePageOne,
				titlePageTwo);		
		pager.setAdapter(pagerAdapter);
     
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.general_options, menu);
		return true;

	}
	
	/** Metodo controlador de acciones del menu general **/
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		Intent intent;
		switch (item.getItemId()) {		
		case R.id.action_close_session:
			
			SharedPreferences prefs = getSharedPreferences("usuario",
					Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = prefs.edit();
			editor.putString("USER", "admin");
			editor.putInt("TYPE",1);
			editor.putBoolean("REMEMBERME", false);
			editor.commit();
			
			intent = new Intent(this, LoginActivity.class);
			this.finish();
			this.startActivity(intent);
			
			break;
		case R.id.action_exit_app:
			this.finish();
			break;
		case R.id.action_to_home:
			intent = new Intent(this, HomeActivity.class);
			this.finish();
			this.startActivity(intent);
			break;			
		default:
			break;
		}

		return super.onContextItemSelected(item);
	}

}
